<?php

namespace app\admin\controller\system\config;

use app\common\controller\AdminController;
use app\services\system\config\SystemConfigService;
use services\CacheService;
use think\App;

/**
 * 系统配置
 * Class SystemConfig
 * @package app\admin\controller\system\config
 */
class SystemConfig extends AdminController
{
    private SystemConfigService $service;

    /**
     * SystemConfig constructor.
     *
     * @param App                 $app
     * @param SystemConfigService $service
     */
    public function __construct(App $app, SystemConfigService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if ($this->request->isAjax()) {

            $where = $this->request->params([
                ['tab_id', 0],
                ['status', -1]
            ]);
            if (!$where['tab_id']) {
                $this->error('参数错误');
            }
            if ($where['status'] == -1) {
                unset($where['status']);
            }
            return layuiTabSuc($this->service->getConfigList($where));
        }
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @param $type
     *
     * @return \think\Response
     */
    public function create()
    {
        $this->assign('tab_id', $this->request->get('tab_id/d'));
        return $this->fetch();
    }

    /**
     * Description:
     *
     */
    public function save()
    {
        $data = $this->request->params([
            'menu_name',
            'type',
            'input_type',
            'config_tab_id',
            'parameter',
            'date_type',
            'upload_type',
            'required',
            'width',
            'high',
            'value',
            'info',
            'desc',
            'sort',
            'status',
        ]);
        if (!$data['info']) {
            $this->error('请输入配置名称');
        }
        if (!$data['menu_name']) {
            $this->error('请输入字段名称');
        }
        if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
            if (!$data['parameter']) {
                $this->error('请输入配置参数');
            }
            $this->service->valiDateRadioAndCheckbox($data);
        }
        $config = $this->service->getOne(['menu_name' => $data['menu_name']])->toArray();
        if ($config) {
            $this->service->update($config['id'], $data, 'id');
        } else {
            $this->service->save($data);
        }
        CacheService::clear();
        $this->success(($config ? '修改' : '添加') . '配置成功!');
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     *
     * @return \think\Response
     */
    public function edit($id)
    {
        $this->assign('row', $this->service->get($id));
        return $this->fetch();
    }

    /**
     * Description: 删除指定资源
     *
     * @param $id
     */
    public function delete($id)
    {
        if (!$this->service->delete($id))
            $this->error('删除失败,请稍候再试!');
        else {
            CacheService::clear();
            $this->success('删除成功!');
        }
    }

    /**
     * 修改状态
     *
     * @param $id
     * @param $status
     *
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if ($status == '' || $id == 0) {
            $this->error('参数错误');
        }
        $this->service->update($id, ['status' => $status]);
        CacheService::clear();
        $this->success($status == 0 ? '隐藏成功' : '显示成功');
    }

    public function basicsConfigList()
    {
        $tabId = $this->request->get('tab_id', 0);
        if (!$tabId) {
            $this->error('参数错误');
        }
        $this->success($this->service->getBasicConfig($tabId));
    }
}
